﻿<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="ClientTitle" ContentPlaceHolderID="TitleContent" runat="server">
	Окно связи с оператором
</asp:Content>
<asp:Content ID="ClientHead" ContentPlaceHolderID="HeadContent" runat="server">
    
</asp:Content>

<asp:Content ID="ClientBody" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        /*$(function () {
            $("#first").click(FirstMess());
            $("#send").click(SendMess());
        });*/
        unload = false;
        firstcallback = true;
        /*$(function () {
            $("body").unload(Disconnect());
            window.onbeforeunload = function () {
                Disconnect();
                alert('on before unload');
            }
        });*/
        function FirstMess() {
            $.post("/Server/SendMessC/", { mess: $("#mess").val() }, GetCallback, "json");            
            $("#first").css("display", "none");
            $("#send").css("display", "inline");
            $("#mess").val("");
            //alert("First");
            
        }
        function SendMess() {
            $.post("/Server/SendMessC/", { mess: $("#mess").val() }, GetCallback, "json");
            $("#mess").val("");
            //alert("send");
        }
        function GetMess() {
            setTimeout(GetMess, 3000);
            $.post("/Server/GetMessC", null, GetCallback, "json");
            //alert("Get");
        }
        function GetCallback(data) {
                if(data != "")
                $("#chat").append("<p>" + data.message + "</p>");
            if (firstcallback) {
                GetMess();
                firstcallback = false;
            }
            
        }
        function Disconnect() {
            if (unload) {
                $.post("/Server/ClientDisconnect", null, GetCallbackDisconnect, "json");
            }
            else unload = true;
        }
        
        function GetCallbackDisconnect(data)
        {
            if (data.dis) {
                //alert("Bye!!!");
                window.close();
            }
        }
        //$(window).unload(Disconnect());
        //window.onunload(Disconnect());
    </script>
    <div id="chat">
    </div><br />    
    <input type="text" id="mess" />
    <input type="button" id="send" value="Отправить" style="display:none" onclick="SendMess()" />    
    <input type="button" id="first" value="Вызвать оператора" onclick="FirstMess()" />
    <input type="button" id="disconnect" value="Выход" onclick="Disconnect()" />
    <p><%= Html.Encode(ViewData["message"]) %></p>
</asp:Content>
